//
//  UIButton+Extension.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/17.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit

extension UIButton {
    //便利构造方法
    convenience init(title: String, titleColor: UIColor, titleFont: CGFloat, backImageName: String, imageName: String? = nil) {
        self.init()
        //设置文字
        setTitle(title, forState: .Normal)
        //设置字体颜色
        setTitleColor(titleColor, forState: .Normal)
        //设置字体大小
        titleLabel?.font = UIFont.systemFontOfSize(titleFont)
        if imageName == nil {
            //设置背景图片
            setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
            setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Selected)
        }else {
            //设置图片
            setImage(UIImage(named: imageName!), forState: .Normal)
        }
        //设置自适应布局
        sizeToFit()
    }
    
    convenience init(imageName: String, backImageName: String) {
        self.init()
        //设置背景图片
        setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Selected)
        //设置图片
        setImage(UIImage(named: imageName), forState: .Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Selected)
    }
}